home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / src / exampleCode / i18n / mnls_wchar.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-08-02  |  3.4 KB  |  128 lines

  1. /*
  2.  * Copyright 1993, 1994, Silicon Graphics, Inc.
  3.  * All Rights Reserved.
  4.  *
  5.  * This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
  6.  * the contents of this file may not be disclosed to third parties, copied or
  7.  * duplicated in any form, in whole or in part, without the prior written
  8.  * permission of Silicon Graphics, Inc.
  9.  *
  10.  * RESTRICTED RIGHTS LEGEND:
  11.  * Use, duplication or disclosure by the Government is subject to restrictions
  12.  * as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
  13.  * and Computer Software clause at DFARS 252.227-7013, and/or in similar or
  14.  * successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
  15.  * rights reserved under the Copyright Laws of the United States.
  16.  */
  17. /*---------------------------------------------------------------------------
  18.  *  mnls_wchar.c  : sample program which shows the use of wide characters
  19.  *                for string manipulation which require locating specific
  20.  *                characters
  21.  *                  Note : this uses MNLS way of cataloging
  22.  *    
  23.  *  this example reads a command line string and outputs two interleaved
  24.  *  strings (not useful in real life, but shows how to locate and
  25.  *  manipulate strings made of wide characters).
  26.  *
  27.  *  Author   : Yusuf Attarwala
  28.  *  Date     : July 93
  29.  *
  30.  *---------------------------------------------------------------------------*/
  31. #include <stdio.h>
  32. #include <stdarg.h>
  33. #include <ctype.h>
  34. #include <string.h>
  35. #include <locale.h>
  36. #include <wctype.h>
  37. #include <pfmt.h>
  38.  
  39. void
  40. usage(pname)
  41. char *pname;
  42. {
  43.     /* MM_ERROR is one of the flags, 
  44.        othe flags can be found in pfmt.h 
  45.     */
  46.  
  47.     (void) pfmt(stderr, MM_ERROR, ":1:Usage: %s string\n", pname);
  48. }
  49.  
  50. myExit()
  51. {
  52.     /* cleanup and exit */
  53.     exit();
  54. }
  55.  
  56. int
  57. main(argc, argv)
  58. register argc;
  59. char **argv;
  60. {
  61.     char *prog;
  62.     register int c;
  63.     char    label[20];
  64.     register wchar_t *s,*o,*p;
  65.     wchar_t *first,*second;
  66.     int i,n;
  67.  
  68.     /*  i18n support */
  69.     /* establish a locale, cause locale database to be read in */
  70.     /* passing empty string will cause setlocale to look
  71.        for an environment variable LANG */
  72.  
  73.     (void)setlocale(LC_ALL, "");
  74.  
  75.     /* open a catalogue for messages, etc */
  76.     prog = (prog = strrchr(argv[0], '/')) ? prog+1 : argv[0];
  77.     (void) setcat("mnls_wchar.cat");
  78.     (void) sprintf(label, "WC:%s", prog);
  79.     (void) setlabel(label);
  80.  
  81.     /* sanity check */
  82.     if (argc < 2) {
  83.     usage(argv[0]);
  84.     myExit();
  85.     }
  86.  
  87.     /* allocate memory for the wide character string */
  88.     n = strlen(argv[1]);
  89.     if( !(s = (wchar_t *)calloc(n + 1, sizeof(wchar_t)))) {
  90.     (void) pfmt(stderr, MM_ERROR, ":2:Could not allocate memory\n");
  91.     myExit();
  92.     }
  93.  
  94.     /* convert string to wchar_t */
  95.     if(mbstowcs(s, argv[1], n) < 0) {
  96.     (void) pfmt(stderr, MM_ERROR, ":3:Error in creating wide characters\n");
  97.     myExit();
  98.     }
  99.  
  100.     /* allocate memory for second string */
  101.     if( !(p = (wchar_t *)calloc(n + 1, sizeof(wchar_t)))) {
  102.     (void) pfmt(stderr, MM_ERROR, ":2:Could not allocate memory\n");
  103.         myExit();
  104.     }
  105.  
  106.     o = s;
  107.  
  108.     first  = o;
  109.     second = p;
  110.  
  111.     /* collect odd and even letters in different strings */
  112.     for(i=0; *s; s += 2,i++) {
  113.     if (!*(s-1) && i > 1) break;    /* don't skip the null terminator */
  114.     *o++ = *s;
  115.     *p++ = *(s+1);
  116.     }
  117.  
  118.     /* null terminators */
  119.     mbtowc (o,"\0",1);
  120.     mbtowc (p,"\0",1);
  121.  
  122.     /* print out the interleaved strings */
  123.     (void) pfmt(stderr, MM_INFO, ":4:Here's the Output : ");
  124.  
  125.     putws(first);
  126.     putws(second);
  127. }
  128.